package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.CurrencyRateDAO;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.CurrencyRate;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.excel.generate.GenerateExcel;
import com.mbc.rmt.dao.AgentBalanceDAO;
import com.mbc.rmt.dao.AgentCOADAO;
import com.mbc.rmt.dao.AgentDAO;
import com.mbc.rmt.dao.BankDAO;
import com.mbc.rmt.dao.CashPaymentDAO;
import com.mbc.rmt.dao.CashPaymentDetailDAO;
import com.mbc.rmt.dao.General_COADAO;
import com.mbc.rmt.dao.OfficeCOADAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.dao.SentCOADAO;
import com.mbc.rmt.dao.WaitingCOADAO;
import com.mbc.rmt.data.CashPaymentDataList;
import com.mbc.rmt.data.CashPaymentDetailsDataList;
import com.mbc.rmt.entities.Agent;
import com.mbc.rmt.entities.AgentBalance;
import com.mbc.rmt.entities.AgentCOA;
import com.mbc.rmt.entities.Bank;
import com.mbc.rmt.entities.CashPayment;
import com.mbc.rmt.entities.CashPaymentCri;
import com.mbc.rmt.entities.CashPaymentDetails;
import com.mbc.rmt.entities.CashPaymentRpt;
import com.mbc.rmt.entities.DefinePaymentTypeDetail;
import com.mbc.rmt.entities.GeneralLedger;
import com.mbc.rmt.entities.General_COA;
import com.mbc.rmt.entities.OfficeCOA;
import com.mbc.rmt.entities.SentCOA;
import com.mbc.rmt.entities.WaitingCOA;

public class CashPaymentMgr extends AbstractMgr{
	
	private CashPaymentDAO cashpaymentdao;
	private CashPaymentDetailDAO cashpaymentdetaildao;
	private AgentBalanceDAO agentbalanceDAO;
	public static enum STATUS { ADD, UPDATE, DELETE }
	
	public static enum GL_STATUS { CREATE, DELETE }
	public static enum TRAN_TYPE { SIMPLE, DETAIL }
	
	private CurrencyRateDAO currencyRateDAO;
	private WaitingCOADAO waitingCOADAO;
	private OfficeCOADAO officeCOADAO;
	private SentCOADAO sendCOADAO;
	private AgentCOADAO agentCOADAO;
	private AgentDAO agentDAO;
	private BankDAO bankDAO;
	private General_COADAO generalCOADAO;
	private LogDAO logDAO;
	
	public CashPaymentMgr() {
	}

	public CashPaymentMgr(String sessionId) {
		this.sessionId = sessionId;
	}
	
	
	public String create(CashPayment info,ArrayList<CashPaymentDetails>infodetaillist,ArrayList<Long>delete_cashpaymentdetaillist,ArrayList<Long>definepaymentdetaillist) throws Exception {
		openConnection();
		try{
			beginTransaction();
			String voucher = "";
			cashpaymentdao = RemittanceDAOFactory.getCashPaymentDAO(dbManager
					.getConnection());
			cashpaymentdetaildao = RemittanceDAOFactory.getCashPaymentDetailDAO(dbManager.getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			CashPayment l_info = new CashPayment();
			CashPaymentDetails l_detail=new CashPaymentDetails();
			if(info.getSlipID() == 0){//For Save
				l_info = new CashPayment();
				l_info = cashpaymentdao.find(info.getSlipID());			
				if (l_info != null) {
					throw new Exception("Invoice ID already exists!");
				
				}else{						
					
					if(info.getTrtype() == 1 )
						GoToAgentBalance(STATUS.ADD, info);

					//Add Header
					info.setSlipID(KeyGenerator.generateKey(getConnection()));
					if(info.getTrtype() == 1)
						voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.CASH_PAYMENT_BY_AGENT_N_BANK, info.getTrdate(), info.getDefaultbranchID(), getConnection());
					if(info.getTrtype() == 2)
						voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.CASH_PAYMENT_BY_SENT, info.getTrdate(), info.getDefaultbranchID(), getConnection());
					if(info.getTrtype() == 3)
						voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.CASH_PAYMENT_BY_OFFICE, info.getTrdate(), info.getDefaultbranchID(), getConnection());
					if(info.getTrtype() == 4)
						voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.CASH_PAYMENT_BY_WAITING, info.getTrdate(), info.getDefaultbranchID(), getConnection());
					
					info.setSlipno(voucher);
					
					gotoGL(info,infodetaillist, GL_STATUS.CREATE, TRAN_TYPE.DETAIL);
					cashpaymentdao.create(info);
					
					Log l_logentity = new Log();
					l_logentity.setTablename("CashPayment");
					l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					l_logentity.setKey(info.getSlipID());
					l_logentity.setUserid(info.getCreatedBy());
					l_logentity.setLogstatus(Log.__SAVE_DATA);
					logDAO.getData(l_logentity);
											
					//Add Detail
					
					for(int l=0;l<infodetaillist.size();l++){
						//Start Looping Here
						l_detail=new CashPaymentDetails();
						l_detail=infodetaillist.get(l);
						
						DefinePaymentTypeDetail l_dptdetail = new DefinePaymentTypeDetail();
						
/*						l_dptdetail.setDetailId(l_detail.getDptdetailid());
						l_dptdetail.setPaidamount(l_detail.getApplyamt());
						l_dptdetail.setTranStatus((byte)1);
						cashpaymentdetaildao.updateTranStatusDPD(l_dptdetail);
						
						l_dptdetail = new DefinePaymentTypeDetail();*/
						l_dptdetail.setPaidamount(cashpaymentdetaildao.finddefinepaidamt(l_detail.getDptdetailid()).getPaidedamount());
						l_dptdetail.setPaidamount(l_dptdetail.getPaidamount() + l_detail.getApplyamt());	
						l_dptdetail.setTranStatus((byte)1);
						l_dptdetail.setDetailId(l_detail.getDptdetailid());
						cashpaymentdetaildao.updateTranStatusDPD(l_dptdetail);
						
						l_detail.setSlipdetailID(KeyGenerator.generateKey(getConnection()));
						l_detail.setSlipID(info.getSlipID());
						
						cashpaymentdetaildao.create(l_detail);
						
						l_logentity.setTablename("CashPaymentDetails");
						l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
						l_logentity.setKey(l_detail.getSlipdetailID());
						l_logentity.setLogstatus(Log.__SAVE_DATA);
						logDAO.getData(l_logentity);
					
					}//End Looping Here
				}
				
			}
			
			else
			{
				
				cashpaymentdao = RemittanceDAOFactory.getCashPaymentDAO(dbManager.getConnection());
				cashpaymentdao.update(info);
				
				Log l_logentity = new Log();
				l_logentity.setTablename("CashPayment");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(info.getSlipID());
				l_logentity.setUserid(info.getCreatedBy());
				l_logentity.setLogstatus(Log.__UPDATE_DATA);
				logDAO.getData(l_logentity);
					
				//gotoGL(info,infodetaillist, GL_STATUS.CREATE, TRAN_TYPE.DETAIL);
				
				if(info.getTrtype() == 1)
					GoToAgentBalance(STATUS.UPDATE, info);
				//Delete detail
				if(info.getTrtype() == 1 || info.getTrtype() == 2){
					if(definepaymentdetaillist.size() != 0)
						for(long key:definepaymentdetaillist) {
							
							DefinePaymentTypeDetail l_dptdetail = new DefinePaymentTypeDetail();
							l_detail = new CashPaymentDetails();
							l_detail = cashpaymentdetaildao.find(0,key);
	//						l_detail = cashpaymentdetaildao.finddefinepaidamt(key);
							if(l_detail != null)
							{
								l_dptdetail.setPaidamount(cashpaymentdetaildao.finddefinepaidamt(l_detail.getDptdetailid()).getPaidedamount());
								l_dptdetail.setPaidamount(l_dptdetail.getPaidamount()-l_detail.getApplyamt());
								l_dptdetail.setTranStatus((byte)0);
								l_dptdetail.setDetailId(key);
								cashpaymentdetaildao.updateTranStatusDPD(l_dptdetail);
							}
						}
	
					if(delete_cashpaymentdetaillist.size() != 0)
						for(long key:delete_cashpaymentdetaillist) {
							cashpaymentdetaildao.deleteRow(key);
						}
				}
				
				for(int l=0;l<infodetaillist.size();l++)
				{//Start Looping Here
					l_detail = new CashPaymentDetails();
					l_detail = infodetaillist.get(l);

					DefinePaymentTypeDetail l_dptdetail = new DefinePaymentTypeDetail();
					CashPaymentDetails l_tempdetail = new CashPaymentDetails();
					/*l_dptdetail.setPaidamount(cashpaymentdetaildao.finddefinepaidamt(l_detail.getDptdetailid()).getPaidedamount());
					l_dptdetail.setDetailId(l_detail.getDptdetailid());
					l_dptdetail.setPaidamount(l_detail.getApplyamt());
					l_dptdetail.setTranStatus((byte)1);
					cashpaymentdetaildao.updateTranStatusDPD(l_dptdetail);*/
					if(info.getTrtype() == 1 || info.getTrtype() == 2){
						l_dptdetail.setPaidamount(cashpaymentdetaildao.finddefinepaidamt(l_detail.getDptdetailid()).getPaidedamount());
						l_tempdetail = cashpaymentdetaildao.find(0,l_detail.getDptdetailid());
						l_dptdetail.setPaidamount((l_dptdetail.getPaidamount()-l_tempdetail.getApplyamt()) + l_detail.getApplyamt());
						l_dptdetail.setTranStatus((byte)1);
						l_dptdetail.setDetailId(l_detail.getDptdetailid());
						cashpaymentdetaildao.updateTranStatusDPD(l_dptdetail);
					}
					else{
						l_tempdetail = cashpaymentdetaildao.find(l_detail.getSlipdetailID(),0);
						long tempdptdetailid = 0;
						long dptdetailid =0 ;
						tempdptdetailid = l_tempdetail.getDptdetailid();
						dptdetailid = l_detail.getDptdetailid();						
						
						if(tempdptdetailid != dptdetailid ){						
							l_dptdetail.setPaidamount(cashpaymentdetaildao.finddefinepaidamt(l_tempdetail.getDptdetailid()).getPaidedamount());
							l_dptdetail.setPaidamount(l_dptdetail.getPaidamount()-l_tempdetail.getApplyamt());
							if(l_dptdetail.getPaidamount() == 0)
								l_dptdetail.setTranStatus((byte)0);
							else
								l_dptdetail.setTranStatus((byte)1);
							l_dptdetail.setDetailId(l_tempdetail.getDptdetailid());
							cashpaymentdetaildao.updateTranStatusDPD(l_dptdetail);
							
							l_dptdetail = new DefinePaymentTypeDetail();
							l_dptdetail.setPaidamount(cashpaymentdetaildao.finddefinepaidamt(l_detail.getDptdetailid()).getPaidedamount());
							l_dptdetail.setPaidamount(l_dptdetail.getPaidamount() + l_detail.getApplyamt());	
							l_dptdetail.setTranStatus((byte)1);
							l_dptdetail.setDetailId(l_detail.getDptdetailid());
							cashpaymentdetaildao.updateTranStatusDPD(l_dptdetail);
						}
						else{
							l_dptdetail = new DefinePaymentTypeDetail();
							l_dptdetail.setPaidamount(cashpaymentdetaildao.finddefinepaidamt(l_detail.getDptdetailid()).getPaidedamount());
							l_dptdetail.setPaidamount((l_dptdetail.getPaidamount()-l_tempdetail.getApplyamt()) + l_detail.getApplyamt());	
							l_dptdetail.setTranStatus((byte)1);
							l_dptdetail.setDetailId(l_detail.getDptdetailid());
							cashpaymentdetaildao.updateTranStatusDPD(l_dptdetail);
						}
					}
					
					if(l_detail.getSlipdetailID() == 0)
					{
						l_detail.setSlipdetailID(KeyGenerator.generateKey(getConnection()));
						l_detail.setSlipID(info.getSlipID());
						cashpaymentdetaildao.create(l_detail);		
						
						l_logentity.setTablename("CashPaymentDetails");
						l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
						l_logentity.setKey(l_detail.getSlipID());
						l_logentity.setLogstatus(Log.__SAVE_DATA);
						logDAO.getData(l_logentity);
					}
					else
						cashpaymentdetaildao.update(l_detail);	
					
					l_logentity.setTablename("CashPaymentDetails");
					l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					l_logentity.setKey(l_detail.getSlipdetailID());
					l_logentity.setLogstatus(Log.__UPDATE_DATA);
					logDAO.getData(l_logentity);

				}//End Looping Here
			}
			//return l_DAOSale.add(info);
			
			commintTransaction();
			return voucher;
		} catch (SQLException e) {
			if (DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e
					.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
				rollbackTransaction();
			throw e;
		}
		finally {
			closeConnection();
		}
	}
	
	public void delete(CashPayment info,long userId, Date deletedDate) throws Exception {
		openConnection();
		try {
			cashpaymentdao = RemittanceDAOFactory.getCashPaymentDAO(dbManager
					.getConnection());
			cashpaymentdetaildao = RemittanceDAOFactory.getCashPaymentDetailDAO(dbManager
					.getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			
			if(info.getTrtype() == 1 )
				GoToAgentBalance(STATUS.DELETE, info);
			
			ArrayList<CashPaymentDetails> infodetaillist = new ArrayList<CashPaymentDetails>();
			infodetaillist = cashpaymentdetaildao.getCashPaymentInfolist(info.getSlipID());
			
			for(int i = 0;i<infodetaillist.size();i++)
			{
				CashPaymentDetails l_detail = new CashPaymentDetails();
				l_detail = infodetaillist.get(i);

				DefinePaymentTypeDetail l_dptdetail = new DefinePaymentTypeDetail();
				/*l_dptdetail.setPaidamount(cashpaymentdetaildao.finddefinepaidamt(l_detail.getDptdetailid()).getPaidedamount());
				l_dptdetail.setDetailId(l_detail.getDptdetailid());
				l_dptdetail.setPaidamount(l_detail.getApplyamt());
				l_dptdetail.setTranStatus((byte)1);
				cashpaymentdetaildao.updateTranStatusDPD(l_dptdetail);*/
				
				l_dptdetail.setPaidamount(cashpaymentdetaildao.finddefinepaidamt(l_detail.getDptdetailid()).getPaidedamount());
				l_dptdetail.setPaidamount(l_dptdetail.getPaidamount() - l_detail.getApplyamt());
				if(l_dptdetail.getPaidamount() == 0)
					l_dptdetail.setTranStatus((byte)0);
				else
					l_dptdetail.setTranStatus((byte)1);
				l_dptdetail.setDetailId(l_detail.getDptdetailid());
				cashpaymentdetaildao.updateTranStatusDPD(l_dptdetail);
				
				l_logentity.setTablename("CashPaymentDetails");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(l_detail.getSlipdetailID());
				l_logentity.setLogstatus(Log.__DELETE_DATA);
				logDAO.getData(l_logentity);
			}
			
			l_logentity.setTablename("CashPayment");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(info.getSlipID());
			l_logentity.setUserid(userId);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			cashpaymentdetaildao.delete(info.getSlipID());
			gotoGL(info,infodetaillist, GL_STATUS.DELETE, TRAN_TYPE.DETAIL);
			cashpaymentdao.delete(info);
			
//			GoToAgentBalance(STATUS.DELETE, saving);
			
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public CashPaymentDataList findCashPayment(CashPaymentCri cri) throws Exception {
		openConnection();
		try {
			cashpaymentdao = RemittanceDAOFactory.getCashPaymentDAO(dbManager
					.getConnection());
			cashpaymentdetaildao = RemittanceDAOFactory.getCashPaymentDetailDAO(getConnection());
			
			 CashPaymentDataList l_list = cashpaymentdao.findCashPayment(cri);
			 if(cri.getTrtype() <=4 ) {
				 for(int i = 0; i < l_list.getEntityList().size(); i++) {
					 l_list.getEntityList().get(i).setInfodetaillist(cashpaymentdetaildao.getCashPaymentInfolist(l_list.getEntityList().get(i).getSlipID()));
				 }
			 }
			 return l_list;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}	
	
	
	public CashPayment getCashPayment(CashPayment info) throws Exception {
		openConnection();
		try {
			cashpaymentdao = RemittanceDAOFactory.getCashPaymentDAO(dbManager
					.getConnection());
			cashpaymentdetaildao = RemittanceDAOFactory.getCashPaymentDetailDAO(dbManager
					.getConnection());
			
			CashPayment l_info = new CashPayment();
			
			if(info != null){
				
				l_info = cashpaymentdao.getCashPayment(info);
				if(l_info != null)
					l_info.setInfodetaillist(cashpaymentdetaildao.getCashPaymentInfolist(l_info.getSlipID()));
				
				return l_info;
			}
			else
				return null;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public ArrayList<CashPaymentDetails> getCashPaymentDetaillist(CashPayment info) throws Exception {
		openConnection();
		try {
			cashpaymentdetaildao = RemittanceDAOFactory.getCashPaymentDetailDAO(dbManager
					.getConnection());
			return cashpaymentdetaildao.getCashPaymentDetaillist(info);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
//	CashPaymentDetailDataList
	public CashPaymentDetailsDataList findCashPaymentDetaillist(CashPaymentCri cri) throws Exception {
		openConnection();
		try {
			cashpaymentdetaildao = RemittanceDAOFactory.getCashPaymentDetailDAO(dbManager
					.getConnection());
			return cashpaymentdetaildao.findCashPaymentDetaillist(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void GoToAgentBalance(STATUS Status,CashPayment  entity) throws Exception {
		try {
			agentbalanceDAO = RemittanceDAOFactory.getAgentBalanceDAO(dbManager
					.getConnection());
			AgentBalance l_agent = new AgentBalance();
			if(Status == STATUS.ADD){
				l_agent = new AgentBalance();
				l_agent.setAgent_id(entity.getAgentID());
				l_agent.setCurrencyid(entity.getCurrencyID());
				l_agent = agentbalanceDAO.getInfo(l_agent);
				if(l_agent == null)
				{
					l_agent = new AgentBalance();
					l_agent.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
					l_agent.setAgent_id(entity.getAgentID());
					l_agent.setCurrencyid(entity.getCurrencyID());
					l_agent.setCurrentbalance(entity.getPaymentamt());
					agentbalanceDAO.create(l_agent);
				}
				else
				{
					l_agent.setCurrentbalance(l_agent.getCurrentbalance()+entity.getPaymentamt());
					agentbalanceDAO.update(l_agent);
				}
			}
			else if(Status == STATUS.UPDATE)
			{
				l_agent = new AgentBalance();
				double amt = 0;
				double tempamt = 0;
				CashPayment tempentity = new CashPayment();
				tempentity = cashpaymentdao.getCashPayment(entity);
				amt = entity.getPaymentamt();
				tempamt = tempentity.getPaymentamt();
				long currencyid= entity.getCurrencyID();
				long tempcur = tempentity.getCurrencyID();
				long agent = entity.getAgentID();
				long tempagent = tempentity.getAgentID();
				
				if(tempcur == currencyid && tempagent == agent)
						{
							l_agent.setAgent_id(entity.getAgentID());
							l_agent.setCurrencyid(entity.getCurrencyID());	
							l_agent = agentbalanceDAO.getInfo(l_agent);
							l_agent.setCurrentbalance((l_agent.getCurrentbalance()-tempamt) + amt);
						}
						else
						{
							double amount = 0;
							l_agent.setCurrencyid(tempcur);
							l_agent.setAgent_id(tempagent);
							l_agent = agentbalanceDAO.getInfo(l_agent);
							amount = l_agent.getCurrentbalance() - tempamt;						
							l_agent.setCurrentbalance(amount);
							agentbalanceDAO.update(l_agent);			
							
							l_agent.setAgent_id(entity.getAgentID());
							l_agent.setCurrencyid(entity.getCurrencyID());	
							l_agent = agentbalanceDAO.getInfo(l_agent);
							if(l_agent == null)
							{
								l_agent = new AgentBalance();
								l_agent.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
								l_agent.setAgent_id(entity.getAgentID());
								l_agent.setCurrencyid(entity.getCurrencyID());
								l_agent.setOpeningbalance(0);
								l_agent.setCurrentbalance(amt);
								agentbalanceDAO.create(l_agent);
							}
							else
							{
								if(currencyid == tempcur && agent == tempagent)
										l_agent.setCurrentbalance(amount+amt);
								else
										l_agent.setCurrentbalance(l_agent.getCurrentbalance() + amt);
							}
						}
						agentbalanceDAO.update(l_agent);
			}
			else
			{
				l_agent = new AgentBalance();
				l_agent.setCurrencyid(entity.getCurrencyID());
				l_agent.setAgent_id(entity.getAgentID());
				l_agent = agentbalanceDAO.getInfo(l_agent);
				if(l_agent != null)
				{
					l_agent.setAgent_id(entity.getAgentID());
					l_agent.setCurrencyid(entity.getCurrencyID());
					l_agent.setCurrentbalance(l_agent.getCurrentbalance()-entity.getPaymentamt());					
					agentbalanceDAO.update(l_agent);
				}
			}
		} catch(Exception e) { throw e; }
		/* -- END GL ADD DATA -- */
	} 
	
	public CashPayment findByCode(String code, long branchId)throws Exception{
		openConnection();
		try {
			cashpaymentdao = RemittanceDAOFactory.getCashPaymentDAO(dbManager.getConnection());
			return cashpaymentdao.findByCode(code, branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public CashPaymentDataList  findCashPaymentDataList(CriteriaParams cri)
			throws Exception {
		openConnection();
		try {
			cashpaymentdao = RemittanceDAOFactory.getCashPaymentDAO(dbManager.getConnection());
			return cashpaymentdao.findCashPaymentDataList(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}	
	
	public CashPaymentDataList  findCashPaymentforReceiptNo(CriteriaParams cri)
			throws Exception {
		openConnection();
		try {
			cashpaymentdao = RemittanceDAOFactory.getCashPaymentDAO(dbManager.getConnection());
			return cashpaymentdao.findCashPaymentforReceiptNo(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}	
	
	public CashPayment  findByCodeforRpt(String code, long branchId)throws Exception{
		openConnection();
		try {
			cashpaymentdao = RemittanceDAOFactory.getCashPaymentDAO(dbManager.getConnection());
			return cashpaymentdao.findByCodeforRpt(code, branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	//Report
		public JasperPrint prepareCashPaymentReport(CashPaymentRpt info)
				throws Exception {
			openConnection();
			JasperPrint l_jsPrint = null;
			try {
				cashpaymentdao = RemittanceDAOFactory.getCashPaymentDAO(dbManager.getConnection());
				JRResultSetDataSource l_jrRS = new JRResultSetDataSource(cashpaymentdao.prepareCashPaymentReport(info));
				JasperReport l_jsRpt = null;
				if(info.getChkAgent() == true ){
					if(info.getGroupby().equals("CurrencyID"))
						l_jsRpt = (JasperReport) JRLoader
						.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/CashPaymentCurRpt.jasper");					
					else
						l_jsRpt = (JasperReport) JRLoader
							.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/CashPaymentRpt.jasper");
				}else if(info.getChkType() == true){
					if(info.getGroupby().equals("CurrencyID"))
						l_jsRpt = (JasperReport) JRLoader
						.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/CashPaymentforTypeforCurRpt.jasper");					
					else
						l_jsRpt = (JasperReport) JRLoader
							.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/CashPaymentforTypeRpt.jasper");
				}else {
					if(info.getGroupby().equals("CurrencyID"))
						l_jsRpt = (JasperReport) JRLoader
						.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/CashPaymentCurRpt.jasper");					
					else
						l_jsRpt = (JasperReport) JRLoader
							.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/CashPaymentRpt.jasper");
				}
				
				HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
				l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

			} catch (JRException je) {
				throw je;
			} catch (Exception e) {
				throw e;
			} finally {
				closeConnection();
			}

			return l_jsPrint;

		}
		
		public String getWorkbookForExcelCP(CashPaymentRpt entity, String path)throws Exception {
			openConnection();
			try {
				
				GenerateExcel excelexport = new GenerateExcel("Cash Payment");
				
				excelexport.putData(String.class, (new java.sql.Timestamp(entity.getFromdate().getTime())).toString());
				excelexport.putData(String.class, (new java.sql.Timestamp(entity.getTodate().getTime())).toString());
				excelexport.putData(String.class, entity.getFromSlipID());
				excelexport.putData(String.class, entity.getToSlipID());
				excelexport.putData(String.class, entity.getFromBranchID());
				excelexport.putData(String.class, entity.getToBranchID());
				excelexport.putData(String.class, entity.getFromAgentID());
				excelexport.putData(String.class, entity.getToAgentID());
				excelexport.putData(String.class, entity.getFromTypeID());
				excelexport.putData(String.class, entity.getToTypeID());
				excelexport.putData(String.class, entity.getFromUserID());
				excelexport.putData(String.class, entity.getToUserID());
				excelexport.putData(String.class, entity.getFromCurrencyID());
				excelexport.putData(String.class, entity.getToCurrencyID());
				excelexport.putData(String.class, entity.getFromReceiptNo());
				excelexport.putData(String.class, entity.getToReceiptNo());
				excelexport.putData(String.class, entity.getFromBankCheque());
				excelexport.putData(String.class, entity.getToBankCheque());
				excelexport.putData(String.class, entity.getGroupby());
				excelexport.putData(Long.class, entity.getBranchid());	
				
				excelexport.putField("No");
				excelexport.putField("SlipNo", "Slip No");
				excelexport.putField("type_code", "Type Code");
				excelexport.putField("InvoiceNo", "Invoice No");
				excelexport.putField("ReceiptNo", "Receipt No");
				excelexport.putField("Sender");
				excelexport.putField("CurrencyCode", "Currency Code");
				excelexport.putField("ApplyAmount", "Apply Amount");
				excelexport.putField("Receiver");
				excelexport.putField("Address");
				excelexport.putField("UserName", "User Name");
				excelexport.putField("BankCode", "Bank Code");
				
				return excelexport.generate(getConnection(), true, "dbo.sp_cr_CashPaymentRpt", path);
				
				/*cashpaymentdao = RemittanceDAOFactory.getCashPaymentDAO(getConnection());
				ResultSet l_rs = cashpaymentdao.prepareCashPaymentReport(entity);
				
				int columncount = l_rs.getMetaData().getColumnCount();
				
				HSSFWorkbook l_workbook = new HSSFWorkbook();
				HSSFSheet l_Sheet = l_workbook.createSheet("data");
				
				int l_rowNum = 0, l_cellNum = 0;
				HSSFRow l_headerRow = l_Sheet.createRow(l_rowNum++);
				
				HSSFCell l_cellNo = l_headerRow.createCell(l_cellNum++);
				l_cellNo.setCellValue("No.");
				
				HSSFCell l_cellSlipNo = l_headerRow.createCell(l_cellNum++);
				l_cellSlipNo.setCellValue("Slip No");
				
				HSSFCell l_cellAgentCode = l_headerRow.createCell(l_cellNum++);
				l_cellAgentCode.setCellValue("Agent Code");
				
				HSSFCell l_cellAgentName = l_headerRow.createCell(l_cellNum++);
				l_cellAgentName.setCellValue("Agent Name");
				
				HSSFCell l_cellTypeCode = l_headerRow.createCell(l_cellNum++);
				l_cellTypeCode.setCellValue("Type Code");
				
				HSSFCell l_cellInvoiceNo = l_headerRow.createCell(l_cellNum++);
				l_cellInvoiceNo.setCellValue("Invoice No");
				
				HSSFCell l_cellReceiptNo = l_headerRow.createCell(l_cellNum++);
				l_cellReceiptNo.setCellValue("Receipt No");
				
				HSSFCell l_cellSender = l_headerRow.createCell(l_cellNum++);
				l_cellSender.setCellValue("Sender");
				
				HSSFCell l_cellCurrencyCode = l_headerRow.createCell(l_cellNum++);
				l_cellCurrencyCode.setCellValue("Currency Code");
				
				HSSFCell l_cellApplyAmount = l_headerRow.createCell(l_cellNum++);
				l_cellApplyAmount.setCellValue("Apply Amount");
				
				HSSFCell l_cellReceiver = l_headerRow.createCell(l_cellNum++);
				l_cellReceiver.setCellValue("Receiver");
				
				HSSFCell l_cellAddress = l_headerRow.createCell(l_cellNum++);
				l_cellAddress.setCellValue("Address");
				
				HSSFCell l_cellUserName = l_headerRow.createCell(l_cellNum++);
				l_cellUserName.setCellValue("User Name");
				
				HSSFCell l_cellBank = l_headerRow.createCell(l_cellNum++);
				l_cellBank.setCellValue("Bank Code");
				
				while(l_rs.next()) {
					HSSFRow l_row = l_Sheet.createRow(l_rowNum++);
					l_cellNum = 0;
					
					for(int i = 0; i < columncount; i++) {
						
						String columnName = l_rs.getMetaData().getColumnName(i + 1);
						
						if(columnName.equals("SlipNo")) {
							l_cellNo = l_row.createCell(l_cellNum++);
							l_cellNo.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("agent_code")) {
							l_cellAgentCode = l_row.createCell(l_cellNum++);
							l_cellAgentCode.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("name")) {
							l_cellAgentName = l_row.createCell(l_cellNum++);
							l_cellAgentName.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("type_code")) {
							l_cellTypeCode = l_row.createCell(l_cellNum++);
							l_cellTypeCode.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("InvoiceNo")) {
							l_cellInvoiceNo = l_row.createCell(l_cellNum++);
							l_cellInvoiceNo.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("ReceiptNo")) {
							l_cellReceiptNo = l_row.createCell(l_cellNum++);
							l_cellReceiptNo.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("Sender")) {
							l_cellSender = l_row.createCell(l_cellNum++);
							l_cellSender.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("CurrencyCode")) {
							l_cellCurrencyCode = l_row.createCell(l_cellNum++);
							l_cellCurrencyCode.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("ApplyAmount")) {
							l_cellApplyAmount = l_row.createCell(l_cellNum++);
							l_cellApplyAmount.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("Receiver")) {
							l_cellReceiver = l_row.createCell(l_cellNum++);
							l_cellReceiver.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("Address")) {
							l_cellAddress = l_row.createCell(l_cellNum++);
							l_cellAddress.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("UserName")) {
							l_cellUserName = l_row.createCell(l_cellNum++);
							l_cellUserName.setCellValue(l_rs.getString(i + 1));
						} else if(columnName.equals("BankCode")) {
							l_cellBank = l_row.createCell(l_cellNum++);
							l_cellBank.setCellValue(l_rs.getString(i + 1));
						}
					}
				}
				
				return l_workbook;*/
			} catch(Exception ex) {
				throw ex;
			} finally {
				closeConnection();
			}
		}
		
		public void gotoGL(CashPayment info,ArrayList<CashPaymentDetails>infodetaillist, GL_STATUS status, TRAN_TYPE trtype) throws Exception {
		try{	
			currencyRateDAO = CommonDAOFactory.getCurrencyRateDAO(getConnection());
			officeCOADAO = RemittanceDAOFactory.getOfficeCOADAO(getConnection());
			waitingCOADAO = RemittanceDAOFactory.getWaitingCOADAO(getConnection());
			sendCOADAO = RemittanceDAOFactory.getSentCOADAO(getConnection());
			agentCOADAO = RemittanceDAOFactory.getAgentCOADAO(getConnection());
			agentDAO = RemittanceDAOFactory.getAgentDAO(getConnection());
			bankDAO = RemittanceDAOFactory.getBankDAO(getConnection());
			generalCOADAO = RemittanceDAOFactory.getGeneral_COADAO(getConnection());		
			
			GeneralLedgerMgr l_glmgr = new GeneralLedgerMgr(this.sessionId);
			
			if(status == GL_STATUS.DELETE){
				if(info.getTrtype() == 1 )
					l_glmgr.deleteGL(info.getSlipno() + "-PA");
				else if(info.getTrtype() == 2)
					l_glmgr.deleteGL(info.getSlipno() + "-PS");
				else if(info.getTrtype() == 3)
					l_glmgr.deleteGL(info.getSlipno() + "-PO");
				else if(info.getTrtype() == 4)
					l_glmgr.deleteGL(info.getSlipno() + "-PW");
			}		
			
			if(info.getTrtype() == 1 )
				l_glmgr.deleteGL(info.getSlipno() + "-PA");
			else if(info.getTrtype() == 2)
				l_glmgr.deleteGL(info.getSlipno() + "-PS");
			else if(info.getTrtype() == 3)
				l_glmgr.deleteGL(info.getSlipno() + "-PO");
			else if(info.getTrtype() == 4)
				l_glmgr.deleteGL(info.getSlipno() + "-PW");
		
			if(status == GL_STATUS.CREATE  && trtype == TRAN_TYPE.DETAIL ) {
				Agent l_agent= new Agent();
				AgentCOA l_agentCOA = new AgentCOA();
				if(info.getAgentID() != null){
					 l_agent = agentDAO.findByCode(info.getAgentcode(),info.getDefaultbranchID());				
					 l_agentCOA = agentCOADAO.findByCode(l_agent.getAccountset_code(), info.getDefaultbranchID());
				}
				General_COA l_generalCOA = generalCOADAO.find(info.getDefaultbranchID());				
				
				Bank l_Bank = new Bank();
				if(info.getBankID() != null){
					l_Bank = bankDAO.findByCode(info.getBankcode(), info.getDefaultbranchID());
				}
				
				for(int i = 0; i <infodetaillist.size(); i++) {
					GeneralLedger _glHeader = new GeneralLedger();
					//_glHeader.setGlKey(KeyGenerator.generateKey(getConnection()));
					_glHeader.setGlKey(l_glmgr.getGLKey());
					
					if(info.getTrtype() == 1 )
						_glHeader.setSlip_ID(info.getSlipno() + "-PA");
					else if(info.getTrtype() == 2)
						_glHeader.setSlip_ID(info.getSlipno() + "-PS");
					else if(info.getTrtype() == 3)
						_glHeader.setSlip_ID(info.getSlipno() + "-PO");
					else if(info.getTrtype() == 4)
						_glHeader.setSlip_ID(info.getSlipno() + "-PW");
					
					_glHeader.setSlipDate(info.getTrdate());
					_glHeader.setSystemDate(new Date());
					_glHeader.setStatus("JH");
					_glHeader.setSrNo(1);
					_glHeader.setOffice_ID(info.getDefaultbranchID());				

					CurrencyRate l_currencyrate = currencyRateDAO.getCurrencyRateFromSourcetoHome(infodetaillist.get(i).getCurrencyID(),info.getDefaultbranchID());
					if(info.getTrtype() == 1){
						
						GeneralLedger _glDebit = new GeneralLedger();
						//_glDebit.setGlKey(KeyGenerator.generateKey(getConnection()));
						_glDebit.setGlKey(l_glmgr.getGLKey());
						_glDebit.setSlip_ID(info.getSlipno() + "-PA");
						_glDebit.setSlipDate(info.getTrdate());
						_glDebit.setSystemDate(new Date());
						if(info.getBankID() == null){
							_glDebit.setCoaKey(l_agentCOA.getAccpayable());
							_glDebit.setDepartmentKey(l_agentCOA.getAccpayable_dept());
						}else{
							_glDebit.setCoaKey(l_Bank.getCoaid());
							_glDebit.setDepartmentKey(l_Bank.getDepartmentid());
						}
						_glDebit.setCurrencyKey(infodetaillist.get(i).getCurrencyID());
						_glDebit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
						_glDebit.setExchangeRate(l_currencyrate.getRate());
						_glDebit.setSourceAmount(infodetaillist.get(i).getApplyamt());
						
						if(l_currencyrate.getRoper().equals("M"))
							_glDebit.setHomeAmount(infodetaillist.get(i).getApplyamt() * l_currencyrate.getRate());
						else
							_glDebit.setHomeAmount(infodetaillist.get(i).getApplyamt() / l_currencyrate.getRate());
						
						_glDebit.setStatus("JPA");
						_glDebit.setSrNo((i + 1));
						_glDebit.setOffice_ID(info.getDefaultbranchID());						
						l_glmgr.createGL(_glDebit);	
						
						GeneralLedger _glCredit = new GeneralLedger();
						//_glCredit.setGlKey(KeyGenerator.generateKey(getConnection()));
						_glCredit.setGlKey(l_glmgr.getGLKey());
						_glCredit.setSlip_ID(info.getSlipno() + "-PA");
						_glCredit.setSlipDate(info.getTrdate());
						_glCredit.setSystemDate(new Date());
						if(info.getBankID() == null){
							_glCredit.setCoaKey(l_generalCOA.getCashAccountID());
							_glCredit.setDepartmentKey(l_generalCOA.getCashDepartmentID());
						}else{
							_glCredit.setCoaKey(l_Bank.getCoaid());
							_glCredit.setDepartmentKey(l_Bank.getDepartmentid());
						}			
						_glCredit.setCurrencyKey(infodetaillist.get(i).getCurrencyID());
						_glCredit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
						_glCredit.setExchangeRate(l_currencyrate.getRate());
						_glCredit.setSourceAmount(-(infodetaillist.get(i).getApplyamt()));
						
						if(l_currencyrate.getRoper().equals("M"))
							_glCredit.setHomeAmount(-(infodetaillist.get(i).getApplyamt() * l_currencyrate.getRate()));
						else
							_glCredit.setHomeAmount(-(infodetaillist.get(i).getApplyamt() / l_currencyrate.getRate()));
						
						_glCredit.setStatus("JPA");
						_glCredit.setSrNo((i + 2));
						_glCredit.setOffice_ID(info.getDefaultbranchID());
						
						l_glmgr.createGL(_glCredit);
					}
					else if(info.getTrtype() == 2){
						SentCOA l_sendcoa = sendCOADAO.findByCode(infodetaillist.get(i).getType_code(), info.getDefaultbranchID());
						if(l_sendcoa == null)
							throw new Exception("Please fill Sent COA");
						
						GeneralLedger _glDebit = new GeneralLedger();
						//_glDebit.setGlKey(KeyGenerator.generateKey(getConnection()));
						_glDebit.setGlKey(l_glmgr.getGLKey());
						_glDebit.setSlip_ID(info.getSlipno() + "-PS");
						_glDebit.setSlipDate(info.getTrdate());
						_glDebit.setSystemDate(new Date());
						_glDebit.setCoaKey(l_sendcoa.getAccpayable());
						_glDebit.setDepartmentKey(l_sendcoa.getAccpayable_dept());
						_glDebit.setCurrencyKey(infodetaillist.get(i).getCurrencyID());
						_glDebit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
						_glDebit.setExchangeRate(l_currencyrate.getRate());
						_glDebit.setSourceAmount(infodetaillist.get(i).getApplyamt());
						
						if(l_currencyrate.getRoper().equals("M"))
							_glDebit.setHomeAmount(infodetaillist.get(i).getApplyamt() * l_currencyrate.getRate());
						else
							_glDebit.setHomeAmount(infodetaillist.get(i).getApplyamt() / l_currencyrate.getRate());
						
						_glDebit.setStatus("JPS");
						_glDebit.setSrNo((i + 1));
						_glDebit.setOffice_ID(info.getDefaultbranchID());						
						l_glmgr.createGL(_glDebit);	
						
						GeneralLedger _glCredit = new GeneralLedger();
						//_glCredit.setGlKey(KeyGenerator.generateKey(getConnection()));
						_glCredit.setGlKey(l_glmgr.getGLKey());
						_glCredit.setSlip_ID(info.getSlipno() + "-PS");
						_glCredit.setSlipDate(info.getTrdate());
						_glCredit.setSystemDate(new Date());
						if(info.getBankID() == null){
							_glCredit.setCoaKey(l_generalCOA.getCashAccountID());
							_glCredit.setDepartmentKey(l_generalCOA.getCashDepartmentID());
						}else{
							_glCredit.setCoaKey(l_Bank.getCoaid());
							_glCredit.setDepartmentKey(l_Bank.getDepartmentid());
						}			
						_glCredit.setCurrencyKey(infodetaillist.get(i).getCurrencyID());
						_glCredit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
						_glCredit.setExchangeRate(l_currencyrate.getRate());
						_glCredit.setSourceAmount(-(infodetaillist.get(i).getApplyamt()));
						
						if(l_currencyrate.getRoper().equals("M"))
							_glCredit.setHomeAmount(-(infodetaillist.get(i).getApplyamt() * l_currencyrate.getRate()));
						else
							_glCredit.setHomeAmount(-(infodetaillist.get(i).getApplyamt() / l_currencyrate.getRate()));
						
						_glCredit.setStatus("JPS");
						_glCredit.setSrNo((i + 2));
						_glCredit.setOffice_ID(info.getDefaultbranchID());
						
						l_glmgr.createGL(_glCredit);
					}else if(info.getTrtype() == 3 || info.getTrtype() == 4){
						OfficeCOA l_officecoa = new OfficeCOA();
						WaitingCOA l_waitingcoa = new WaitingCOA();
						if(infodetaillist.get(i).getType_code().equals("O"))
							 l_officecoa = officeCOADAO.findByCode(infodetaillist.get(i).getType_code(),info.getDefaultbranchID());
						else if(infodetaillist.get(i).getType_code().equals("W"))
							l_waitingcoa = waitingCOADAO.findByCode(infodetaillist.get(i).getType_code(),info.getDefaultbranchID());
						
						GeneralLedger _glDebit = new GeneralLedger();
						//_glDebit.setGlKey(KeyGenerator.generateKey(getConnection()));
						_glDebit.setGlKey(l_glmgr.getGLKey());
						if(infodetaillist.get(i).getType_code().equals("O"))
							_glDebit.setSlip_ID(info.getSlipno() + "-PO");
						else
							_glDebit.setSlip_ID(info.getSlipno() + "-PW");
						_glDebit.setSlipDate(info.getTrdate());
						_glDebit.setSystemDate(new Date());
						if(infodetaillist.get(i).getType_code().equals("O")){
							_glDebit.setCoaKey(l_officecoa.getAccpayable());
							_glDebit.setDepartmentKey(l_officecoa.getAccpayable_dept());
						}else{
							_glDebit.setCoaKey(l_waitingcoa.getAccpayable());
							_glDebit.setDepartmentKey(l_waitingcoa.getAccpayable_dept());
						}
						_glDebit.setCurrencyKey(infodetaillist.get(i).getCurrencyID());
						_glDebit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
						_glDebit.setExchangeRate(l_currencyrate.getRate());
						_glDebit.setSourceAmount(infodetaillist.get(i).getApplyamt());
						
						if(l_currencyrate.getRoper().equals("M"))
							_glDebit.setHomeAmount(infodetaillist.get(i).getApplyamt() * l_currencyrate.getRate());
						else
							_glDebit.setHomeAmount(infodetaillist.get(i).getApplyamt() / l_currencyrate.getRate());
						
						if(infodetaillist.get(i).getType_code().equals("O"))
							_glDebit.setStatus("JPO");
						else
							_glDebit.setStatus("JPW");
						_glDebit.setSrNo((i + 1));
						_glDebit.setOffice_ID(info.getDefaultbranchID());						
						l_glmgr.createGL(_glDebit);	
						
						GeneralLedger _glCredit = new GeneralLedger();
						//_glCredit.setGlKey(KeyGenerator.generateKey(getConnection()));
						_glCredit.setGlKey(l_glmgr.getGLKey());
						if(infodetaillist.get(i).getType_code().equals("O"))
							_glCredit.setSlip_ID(info.getSlipno() + "-PO");
						else
							_glCredit.setSlip_ID(info.getSlipno() + "-PW");						
							
						_glCredit.setSlipDate(info.getTrdate());
						_glCredit.setSystemDate(new Date());
						if(info.getBankID() == null){
							_glCredit.setCoaKey(l_generalCOA.getCashAccountID());
							_glCredit.setDepartmentKey(l_generalCOA.getCashDepartmentID());
						}else{
							_glCredit.setCoaKey(l_Bank.getCoaid());
							_glCredit.setDepartmentKey(l_Bank.getDepartmentid());
						}			
						_glCredit.setCurrencyKey(infodetaillist.get(i).getCurrencyID());
						_glCredit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
						_glCredit.setExchangeRate(l_currencyrate.getRate());
						_glCredit.setSourceAmount(-(infodetaillist.get(i).getApplyamt()));
						
						if(l_currencyrate.getRoper().equals("M"))
							_glCredit.setHomeAmount(-(infodetaillist.get(i).getApplyamt() * l_currencyrate.getRate()));
						else
							_glCredit.setHomeAmount(-(infodetaillist.get(i).getApplyamt() / l_currencyrate.getRate()));
						
						if(infodetaillist.get(i).getType_code().equals("O"))
							_glCredit.setStatus("JPO");
						else
							_glCredit.setStatus("JPW");
						_glCredit.setSrNo((i + 2));
						_glCredit.setOffice_ID(info.getDefaultbranchID());
						
						l_glmgr.createGL(_glCredit);
					}
					l_glmgr.createGL(_glHeader);
				}
			}	
			} catch(Exception e) { throw e; }		
		}
		
}
